
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include( 'shared.lua' )

ENT.Honk = Sound( "Trainyard.train_horn_everywhere" )
ENT.ShadowParams = {}

function ENT:Initialize()

	self.Entity:SetModel( "models/props/de_train/diesel.mdl" )
	
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_NONE )
	
	self.Entity:StartMotionController()
	
	local phys = self.Entity:GetPhysicsObject()
	
	if ValidEntity( phys ) then
	
		phys:EnableGravity( false )
		phys:Wake()
		
	end
	
	self.Kick = true
	
end

function ENT:SetKick( bool )

	self.Kick = bool

end

function ENT:Think()

	if not ValidEntity( self.Entity:GetOwner() ) or ( self.KilledPlayer and self.KilledPlayer < CurTime() ) then 
	
		if self.Kick and self.KilledPlayer and ValidEntity( self.Entity:GetOwner() ) then
		
			self.Entity:GetOwner():Kick( "Permabanned by the ban train." )
		
		end
	
		self.Entity:Remove()
		
		return
	
	end

	if not self.SoundPlayed and self.Entity:GetPos():Distance( self.Entity:GetOwner():GetPos() ) < 2000 then
	
		self.Entity:EmitSound( self.Honk, 100, 90 )
		self.SoundPlayed = true
	
	end
	
	if self.KillTimer and self.KillTimer < CurTime() then
	
		self.Entity:GetOwner():Kill()
		self.KillTimer = nil
	
	end
	
	if self.Entity:GetPos():Distance( self.Entity:GetOwner():GetPos() ) < 250 and self.Entity:GetOwner():Alive() and not self.KilledPlayer then
	
		self.Entity:GetOwner():SetVelocity( ( self.Entity:GetOwner():GetPos() - self.Entity:GetPos() ):Normalize() * 5000 + Vector(0,0,500) )
		self.KilledPlayer = CurTime() + 8
		self.KillTimer = CurTime() + 0.1
	
	end
	
end

function ENT:PhysicsUpdate( phys, delta )

	phys:Wake()

	if not ValidEntity( self.Entity:GetOwner() ) then return SIM_NOTHING end
	
	if self.Entity:GetOwner():Alive() and not self.KilledPlayer then
	
		self.ShadowParams.pos = self.Entity:GetPos() + ( self.Entity:GetOwner():GetPos() - self.Entity:GetPos() ):Normalize() * 500
		self.ShadowParams.angle = ( self.Entity:GetOwner():GetPos() - self.Entity:GetPos() ):Normalize():Angle()
		
	else
	
		self.ShadowParams.pos = self.Entity:GetPos() + self.Entity:GetForward() * 500
	
	end
	
	self.ShadowParams.secondstoarrive = 1 
	self.ShadowParams.maxangular = 5000 
	self.ShadowParams.maxangulardamp = 6000
	self.ShadowParams.maxspeed = 5000 
	self.ShadowParams.maxspeeddamp = 6000
	self.ShadowParams.dampfactor = 0.4
	self.ShadowParams.teleportdistance = 90000
	self.ShadowParams.deltatime = delta

	phys:ComputeShadowControl( self.ShadowParams )

end

function ENT:PhysicsCollide( data, physobj )
	
end

function ENT:OnTakeDamage( dmginfo )
	
end

function ENT:Use( ply, caller )

end

function ENT:OnRemove()
	
end